﻿using ZYCommon;
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Data;
using Bifrost;
using TextEditor.TextDocument.Document;


namespace TextEditor
{
    public class ZYTextSelect : ZYTextBlock
    {
        private bool bolPopuping = false;
        private bool bolReadonly = false;
        private System.Drawing.Font myFont = null;
        private ArrayList myOptions = new ArrayList();
        private KB_List myOwnerKBList = null;
        public SelectForm selectForm;

        public ZYTextSelect()
        {
            base.bolChildElementsLocked = true;
            Sign = "";
        }

        protected override void AddLastElement()
        {
        }


        public void setMoveStart()
        {
            base.myOwnerDocument.Content.AutoClearSelection = true;
            base.myOwnerDocument.Content.MoveSelectStart((int)(base.Index + 1));
        }
        public KB_Item AddOption(string strText)
        {
            this.SaveList = true;
            KB_Item item = new KB_Item();
            item.ItemText = strText;
            item.ItemValue = strText;
            this.myOptions.Add(item);
            return item;
        }

        public override int AppendKeyValueList(ArrayList myKeyValues)
        {
            myKeyValues.Add(base.ID);
            if (ZYKBBuffer.Instance.Loading)
            {
                myKeyValues.Add(base.Name);
            }
            else
            {
                KB_List kBList = ZYKBBuffer.Instance.GetKBList(this.ListSource);
                myKeyValues.Add((kBList == null) ? "0" : kBList.Name);
            }
            myKeyValues.Add(this.Value);
            return 1;
        }

        protected override bool BeforeInsert(ZYTextElement NewElement)
        {
            return false;
        }

        public override bool CanBeLineEnd()
        {
            return false;
        }

        /// <summary>
        /// 画知识库元素的背景色和下划线
        /// </summary>
        /// <param name="myElement"></param>
        public override void DrawBackGround(ZYTextElement myElement)
        {
            //if (base.bolStandOutBack && !base.myOwnerDocument.Info.Printing)
            //{
            //    base.myOwnerDocument.View.FillRectangle(Color.Red, myElement.Bounds);
            //}
            if (this.bolStandOutBack && !this.OwnerDocument.Info.Printing)
            {
                base.myOwnerDocument.View.DrawLine(SystemPens.WindowText, myElement.Bounds.Left, myElement.Bounds.Bottom - 2, myElement.Bounds.Right, myElement.Bounds.Bottom - 2);
            }

        }

        public override bool FromXML(XmlElement myElement)
        {
            if (base.BaseFromXML(myElement))
            {
                //this.ChildElements.Clear();
                UpdateText2("");
                FromListXML(myElement);
                return true;
            }
            return false;
        }

        private void FromListXML(XmlElement myElement)
        {
            if (myElement != null && myElement.ChildNodes.Count > 0)
            {
                this.ChildElements.Clear();
                ArrayList myList = new ArrayList();
                base.myOwnerDocument.LoadElementsToList(myElement, myList);
                foreach (ZYTextElement element2 in myList)
                {
                    element2.Parent = this;
                }
                this.ChildElements.AddRange(myList);
            }
        }



        //private void FromListXML(XmlElement myElement)
        //{
        //    if (myElement != null)
        //    {
        //        foreach (XmlNode node in myElement.ChildNodes)
        //        {
        //            XmlElement element = node as XmlElement;
        //            if ((element != null) && (element.Name == "option"))
        //            {
        //                KB_Item item = new KB_Item();
        //                item.ItemText = element.GetAttribute("text");
        //                item.ItemValue = element.GetAttribute("value");
        //                this.myOptions.Add(item);
        //            }
        //        }
        //    }
        //}

        //public override bool FromXML(XmlElement myElement)
        //{
        //    if (base.BaseFromXML(myElement))
        //    {
        //        if (StringCommon.HasContent(base.ValueSource) && base.myOwnerDocument.Variables.Contains(base.ValueSource))
        //        {
        //            base.myAttributes.SetValue("text", base.myOwnerDocument.Variables.GetValue(base.ValueSource));
        //        }
        //        this.UpdateText();
        //        this.myOptions.Clear();
        //        this.FromListXML(myElement);
        //        if (ZYKBBuffer.Instance.CanReadKB())
        //        {
        //            this.myOwnerKBList = ZYKBBuffer.Instance.GetKBList(this.ListSource);
        //        }
        //        this.UpdateAttrubute();
        //        return true;
        //    }
        //    return false;
        //}

        internal EventHandler GetSelectedStateChangedHandler()
        {
            if (this.Multiple)
            {
                return new EventHandler(this.SelectedStateChanged);
            }
            return null;
        }

        public override string GetXMLName()
        {
            return "select";
        }

        //public override bool HandleMouseDown(int x, int y, MouseButtons Button)
        //{
        //    if (this.Contains(x, y))
        //    {
        //        this.PopupList();
        //        return true;
        //    }
        //    return false;
        //}


        // public override bool HandleDblClick(int x, int y, MouseButtons Button)
        //{
        //    if (this.Contains(x, y))
        //    {
        //        //base.myOwnerDocument.Content.AutoClearSelection = true;
        //        //base.myOwnerDocument.Content.MoveSelectStart(this);
        //        //base.myOwnerDocument.SetSelectToList(this);                
        //    }
        //    return false;
        //}

        //public override bool HandleMouseMove(int x, int y, MouseButtons Button)
        //{
        //    if (this.Contains(x, y))
        //    {
        //        base.myOwnerDocument.SetCursor(Cursors.Arrow);
        //        string[] strs = this.Sign.Split('|');
        //        if (strs.Length > 0)
        //        {
        //            string TipText = "";
        //            for (int i = strs.Length-1; i >= 0; i--)
        //            {
        //                if (strs[i].Length > 100)
        //                {
        //                    string[] strs2 = strs[i].Split(',');
        //                    for (int j = 0; j < strs2.Length; j++)
        //                    {
        //                        TipText += strs2[j] + "\r\n";
        //                    }
        //                }
        //                else 
        //                TipText += strs[i] + "\r\n";
        //            }
        //            base.myOwnerDocument.SetToolTip(TipText, -1, base.myBounds);
        //        }
        //        base.myOwnerDocument.CurrentHoverElement = this;
        //        return true;
        //    }
        //    return false;
        //}

        public bool HasOptions()
        {
            return ((this.myOptions != null) && (this.myOptions.Count > 0));
        }

        public override bool isField()
        {
            return true;
        }

        public void PopupList()
        {

            this.bolPopuping = true;
            base.myOwnerDocument.Content.AutoClearSelection = true;
            base.myOwnerDocument.Content.CurrentElement = this;
            this.ResetViewState();
            base.myOwnerDocument.RefreshElement(this);
            this.bolPopuping = false;
            base.myOwnerDocument.RefreshElement(this);

        }

        //public override bool RefreshView()
        //{
        //    Color foreColor = this.ForeColor;
        //    foreach (ZYTextChar ch in base.myChildElements)
        //    {
        //        ch.intForeColor = foreColor;
        //        ch.Font = this.Font;
        //    }
        //    return base.RefreshView();
        //}

        public override void ResetViewState()
        {
            if (this.bolPopuping)
            {
                base.bolStandOutBack = true;
            }
            else
            {
                base.ResetViewState();
            }
        }

        private void SelectedStateChanged(object Sender, EventArgs e)
        {
            if (this.Multiple)
            {
                this.SetValue((Sender as ZYPopupList).SelectedObjects, false);
            }
        }

        public bool SetValue(object objValue, bool ChangeSelection)
        {
            bool flag = false;
            if (objValue == null)
            {
                return false;
            }
            string text = this.Text;
            if (ChangeSelection)
            {
                base.myOwnerDocument.Content.AutoClearSelection = true;
                base.myOwnerDocument.Content.MoveSelectStart(this);
            }
            base.myOwnerDocument.BeginContentChangeLog();
            base.myOwnerDocument.BeginUpdate();
            if (this.Multiple)
            {
                ArrayList list = (ArrayList)objValue;
                string itemText = null;
                string itemValue = null;
                if (list.Count > 0)
                {
                    foreach (KB_Item item in list)
                    {
                        if (itemText == null)
                        {
                            itemText = item.ItemText;
                        }
                        else
                        {
                            itemText = itemText + "、" + item.ItemText;
                        }
                        if (itemValue == null)
                        {
                            itemValue = item.ItemValue;
                        }
                        else
                        {
                            itemValue = itemValue + "、" + item.ItemValue;
                        }
                        if (StringCommon.isBlankString(base.Name))
                        {
                            base.Name = item.OwnerList.Name;
                            this.ListSource = item.OwnerList.SEQ;
                        }
                    }
                }
                if (this.Text != itemText)
                {
                    this.Text = itemText;
                    this.Value = itemValue;
                    flag = true;
                }
            }
            else
            {
                KB_Item item2 = objValue as KB_Item;
                if (item2 != null)
                {
                    if (item2.isTemplate())
                    {
                        if (base.myOwnerDocument.OwnerControl != null)
                        {
                            base.myOwnerDocument.OwnerControl.HideSmartTag();
                        }
                        ET_Document myRecord = new ET_Document();
                        myRecord.SetKeyWord(item2.ItemValue);
                        string str4 = null;
                        if (base.myOwnerDocument.DataSource.DBConn.ReadOneRecord(myRecord))
                        {
                            try
                            {
                                ZYTextContainer container = base.myOwnerDocument.CreateElementByXML(ZYTextDocument.GetBodyElement(myRecord.GetDataXML().DocumentElement)) as ZYTextContainer;
                                if (container != null)
                                {
                                    container.ClearSaveLog();
                                    ArrayList myList = new ArrayList();
                                    myList.AddRange(container.ChildElements);
                                    if ((myList.Count > 0) && (myList[myList.Count - 1] is ZYTextParagraph))
                                    {
                                        myList.RemoveAt(myList.Count - 1);
                                    }
                                    base.myParent.InsertRangeBefore(myList, this);
                                    base.myParent.RemoveChild(this);
                                    base.myOwnerDocument.RefreshSize();
                                    base.myOwnerDocument.RefreshElements();
                                    base.myOwnerDocument.RefreshLine();
                                }
                            }
                            catch (Exception exception)
                            {
                                str4 = "导入编号为[" + item2.ItemValue + "]的模板错误!\r\n" + exception.ToString();
                            }
                        }
                        else
                        {
                            str4 = "不存在编号为[" + item2.ItemValue + "]的模板!";
                        }
                        if (str4 != null)
                        {
                            MessageBox.Show(base.myOwnerDocument.OwnerControl, str4, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                        base.myOwnerDocument.EndContentChangeLog();
                        base.myOwnerDocument.EndUpdate();
                        return false;
                    }
                    if (StringCommon.isBlankString(base.Name))
                    {
                        base.Name = item2.OwnerList.Name;
                        this.ListSource = item2.OwnerList.SEQ;
                        base.myOwnerDocument.Modified = true;
                    }
                    if (this.Text != item2.ItemText)
                    {
                        this.Text = item2.ItemText;
                        this.Value = item2.ItemValue;
                        flag = true;
                    }
                }
            }
            if (StringCommon.isBlankString(this.Value))
            {
                this.Value = this.Text;
                flag = true;
            }
            if (flag)
            {
                this.RefreshSize();
                base.myOwnerDocument.RefreshElements();
                base.myOwnerDocument.RefreshLine();
                if (ChangeSelection)
                {
                    base.myOwnerDocument.Content.AutoClearSelection = true;
                    base.myOwnerDocument.Content.MoveSelectStart((int)(base.Index + 1));
                }
                base.myOwnerDocument.Refresh();
                base.myOwnerDocument.Modified = true;
            }
            base.myOwnerDocument.EndUpdate();
            base.myOwnerDocument.EndContentChangeLog();
            return true;
        }

        //public override bool ToXML(XmlElement myElement)
        //{
        //    string str;
        //    string iD;
        //    base.myAttributes.SetValue("value", this.Value);
        //    switch (base.myOwnerDocument.Info.SaveMode)
        //    {
        //        case 0:
        //            base.myAttributes.ToXML(myElement);
        //            if (this.SaveList && (this.myOptions.Count < 100))
        //            {
        //                foreach (KB_Item item in this.myOptions)
        //                {
        //                    XmlElement newChild = myElement.OwnerDocument.CreateElement("option");
        //                    myElement.AppendChild(newChild);
        //                    newChild.SetAttribute("text", item.ItemText);
        //                    newChild.SetAttribute("value", item.ItemValue);
        //                }
        //            }
        //            goto Label_01FD;

        //        case 1:
        //            myElement.SetAttribute("id", base.ID);
        //            myElement.SetAttribute("name", base.Name);
        //            myElement.InnerText = this.Value;
        //            goto Label_01FD;

        //        case 2:
        //            myElement.SetAttribute("id", base.ID);
        //            myElement.SetAttribute("name", base.Name);
        //            myElement.InnerText = this.Value;
        //            goto Label_01FD;

        //        case 3:
        //            str = this.Value;
        //            iD = base.ID;
        //            if (!StringCommon.isBlankString(iD))
        //            {
        //                iD = iD.Trim().Replace(' ', '_');
        //                break;
        //            }
        //            iD = "未命名列表";
        //            break;

        //        default:
        //            goto Label_01FD;
        //    }
        //    if (StringCommon.HasContent(str))
        //    {
        //        foreach (string str3 in str.Split("、".ToCharArray()))
        //        {
        //            XmlElement element2 = myElement.OwnerDocument.CreateElement(iD);
        //            myElement.ParentNode.InsertAfter(element2, myElement);
        //            element2.InnerText = str3;
        //        }
        //        myElement.ParentNode.RemoveChild(myElement);
        //    }
        //    else
        //    {
        //        myElement.InnerText = "";
        //    }
        //Label_01FD:
        //    return true;
        //}

        //internal void UpdateText()
        //{
        //    string text = this.Text;
        //    if (StringCommon.isBlankString(text))
        //    {
        //        text = this.DisplayName;
        //    }
        //    ArrayList c = base.myOwnerDocument.CreateChars(text, this);
        //    base.myChildElements.Clear();
        //    base.myChildElements.AddRange(c);
        //    foreach (ZYTextElement element in base.myChildElements)
        //    {
        //        element.CreatorIndex = base.CreatorIndex;
        //        element.DeleterIndex = base.DeleterIndex;
        //        element.Font = this.Font;
        //    }
        //    this.RefreshSize();
        //    if (!base.myOwnerDocument.Loading)
        //    {
        //        base.RaiseOnChangeEvent();
        //    }
        //    base.myOwnerDocument.ElementsDirty = true;
        //}

        public string DisplayName
        {
            get
            {
                string strData = base.myAttributes.GetString("displayname");
                if (StringCommon.isBlankString(strData))
                {
                    strData = "[" + (StringCommon.isBlankString(base.Name) ? base.ID : base.Name) + "]";
                }
                return strData;
            }
            set
            {
                base.myAttributes.SetValue("displayname", value);
            }
        }

        public override System.Drawing.Font Font
        {
            get
            {
                if (this.myFont == null)
                {
                    this.myFont = base.myOwnerDocument.View._CreateFont(this.FontName, this.FontSize, this.FontBold, this.FontItalic, this.FontUnderLine);
                }
                return this.myFont;
            }
            set
            {
                this.FontName = value.Name;
                this.FontSize = value.Size;
                //  this.Height = value .Height ;
                this.FontBold = value.Bold;
                this.FontItalic = value.Italic;
                this.FontUnderLine = value.Underline;
            }
        }

        public override bool FontBold
        {
            get
            {
                return base.Attributes.GetBool("fontbold");
            }
            set
            {
                base.Attributes.SetValue("fontbold", value);
                this.myFont = null;//******
                //this.myFont = f;
            }
        }

        public override bool FontItalic
        {
            get
            {
                return base.Attributes.GetBool("fontitalic");
            }
            set
            {
                base.Attributes.SetValue("fontitalic", value);
                this.myFont = null;//******
                //this.myFont = f;
            }
        }

        public override string FontName
        {
            get
            {
                return base.Attributes.GetString("fontname");
            }
            set
            {
                base.Attributes.SetValue("fontname", value);
                this.myFont = null;//********
                //this.myFont = f;
            }
        }

        public override float FontSize
        {
            get
            {
                return base.Attributes.GetFloat("fontsize");
            }
            set
            {
                base.Attributes.SetValue("fontsize", value);
                this.myFont = null;
                //this.myFont = f;
            }
        }

        public override bool FontUnderLine
        {
            get
            {
                return base.Attributes.GetBool("fontunderline");
            }
            set
            {
                base.Attributes.SetValue("fontunderline", value);
                this.myFont = null;
                //this.myFont = f;
            }
        }

        public override Color ForeColor
        {
            get
            {
                return base.myAttributes.GetColor("forecolor");
            }
            set
            {
                base.myAttributes.SetValue("forecolor", value);
            }
        }

        public override int Height
        {
            get
            {
                return base.intHeight;
            }
            set
            {
                base.intHeight = value;
            }
        }

        public string ListSource
        {
            get
            {
                string str = base.myAttributes.GetString("listsource");
                return str;
            }
            set
            {
                base.myAttributes.SetValue("listsource", value);
                base.myAttributes.SetValue("id", "kb" + value);
            }
        }

        public bool Multiple
        {
            get
            {
                return base.myAttributes.GetBool("multiple");
            }
            set
            {
                base.myAttributes.SetValue("multiple", value);
            }
        }

        public ArrayList Options
        {
            get
            {
                return this.myOptions;
            }
        }

        public KB_List OwnerKBList
        {
            get
            {
                return this.myOwnerKBList;
            }
            set
            {
                this.myOwnerKBList = value;
                if (value != null)
                {
                    base.Name = value.Name;
                    this.ListSource = value.SEQ;
                }
            }
        }

        public bool Readonly
        {
            get
            {
                return this.bolReadonly;
            }
            set
            {
                this.bolReadonly = value;
            }
        }

        public bool SaveList
        {
            get
            {
                return base.myAttributes.GetBool("savelist");
            }
            set
            {
                base.myAttributes.SetValue("savelist", value);
            }
        }

        public int Source
        {
            get
            {
                return base.myAttributes.GetInt32("source");
            }
            set
            {
                base.myAttributes.SetValue("source", value);
            }
        }

        public string Text
        {
            get
            {
                return this.ToZYString();
            }
            set
            {
                UpdateText2(value);
            }
        }

        private void UpdateText2(string value)
        {
            string text = value;
            //if (StringCommon.isBlankString(text))
            //{
            //    text = this.DisplayName;
            //}
            ArrayList c = new ArrayList();
            c = base.myOwnerDocument.CreateChars(text, this);
            if (c != null)
            {
                base.myChildElements.Clear();
                base.myChildElements.AddRange(c);
                base.myOwnerDocument.ElementsDirty = true;
                this.RefreshSize();
            }
        }

        public string Value
        {
            get
            {
                return base.myAttributes.GetString("value");
            }
            set
            {
                base.myAttributes.SetValue("value", value);
            }
        }


        public override bool WholeElement
        {
            get
            {
                return true;
            }
        }

        public override int Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
            }
        }
    }
}

